<template>
	<view class="">
		<u-navbar title="充值" autoBack placeholder border></u-navbar>
		<view class="flex flex-column align-center justify-center bg-white" style="height: 228rpx;">
			<view class="font-24 text-light6">余额(元)</view>
			<view class="font-weight-bold text-main font-lg">￥{{dataInfo.money}}</view>
		</view>
		
		<view class="p-2 flex flex-column mt-2 bg-white">
			<view class="font-28 text-light6">充值金额</view>
			<view class="mt-2 border-bottom flex align-center">
				<view class="font-28 text-main font-weight-bold">¥</view>
				<view class="ml-3">
					<u-input v-model="value" placeholder="输入金额" border="none"></u-input>
				</view>
			</view>
		</view>
		
		<view v-if="platform==='plus'"  class="p-2 flex align-center mt-2 bg-white" @click="show=true">
			<view class="font-28 text-light6">充值方式</view>
			<view class="font-28 text-main ml-1">{{curIndex|paytype}}</view>
			<view class="ml-auto">
				<u-icon name="arrow-right"></u-icon>
			</view>
		</view>
		
		<commonButton title="立即充值" @click="submint"></commonButton>
		
		<u-popup  :show="show">
			<commonPay :curIndex="curIndex" status="recharge" @click="changepay" @close="show=false"></commonPay>
		</u-popup>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show:false,
				curIndex:0,
				value:'',
				platform:'',
				dataInfo:{}
			}
		},
		filters:{
			paytype(value){
				return value===0?'支付宝':value===1?'微信':'余额'
			}
		},
		onLoad() {
			this.platform = uni.$u.platform
			this.getData()
		},
		
		methods: {
			getData(){
				this.$api.user().then(res=>{
					if(res.code === 1){
						this.dataInfo = res.data
					}
				})
			},
			changepay(index){
				this.curIndex = index
				this.show = false
			},
			submint(){
				if(this.platform === 'mp'){
					let parmar = {
						type:1,
						money:this.value
					}
					this.$api.wxbalancerecharge(parmar).then(res=>{
						if(res.code === 1){
							uni.requestPayment({
								provider:'wxpay',
								timeStamp:res.data.timeStamp,
								nonceStr:res.data.nonceStr,
								package:res.data.package,
								signType:res.data.signType,
								paySign:res.data.paySign,
								success: (res) => {
									uni.showToast({
										title: '充值成功',
										icon:'none'
									});
									
									setTimeout(()=>{
										uni.navigateBack({
											delta: 1
										});
									},500)
								}
							})
						}
					})
					
				}else{
					let parmar = {
						type:this.curIndex===0?'2':'1',
						money:this.value
					}
					this.$api.balanceRecharge(parmar).then(res=>{
						console.log(res)
						if(res.code === 1){
							
							uni.requestPayment({
								provider:this.curIndex===0?'alipay':'wxpay',
								orderInfo:res.data,
								success: (res) => {
									uni.showToast({
										title: '充值成功',
										icon:'none'
									});
									setTimeout(()=>{
										uni.navigateBack({
											delta: 1
										});
									},500)
								}
							})
						}
					})
					
					
					
					
					
				}
			}
		}
	}
</script>

<style>
</style>